package agent

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/agentClient/rpc"
	user "gitee.com/liuxuezhan/ar-platform/rpc-client/loginClient/client"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RebatesSettingUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 返佣配置修改
func NewRebatesSettingUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RebatesSettingUpdateLogic {
	return &RebatesSettingUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RebatesSettingUpdateLogic) RebatesSettingUpdate(req *types.RebatesSettingRequest) (resp *types.Response, err error) {
	token, err := l.svcCtx.LoginRpc.ParseToken(l.ctx, &user.ParseTokenRequest{
		Token: headInfo.GetJwtToken(l.ctx),
	})
	if err != nil {
		logc.Error(l.ctx, err)
		return nil, err
	}
	adminName := token.UserName
	_gameRate := make([]*rpc.GameRate, len(req.GamesRate))
	for _i, _row := range req.GamesRate {
		_gameRate[_i] = &rpc.GameRate{
			GameId: _row.GameId,
			Game:   _row.Game,
			Rate:   _row.Rate,
			Code:   _row.Code,
		}
	}
	_, err = l.svcCtx.AgentRPC.RebatesSettingUpdate(l.ctx, &rpc.AgentRebatesSettingRequest{
		Id:           req.Id,
		GamesRate:    _gameRate,
		LevelRate:    req.LevelRate,
		LevelLimit:   req.LevelLimit,
		ConditionNum: req.ConditionNum,
		Remark:       req.Remark,
		Admin:        adminName,
	})
	if err != nil {
		logc.Error(l.ctx, err)
		return nil, err
	}
	resp = &types.Response{}
	return
}
